﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace FXnaMet
{
    [Serializable]
    public class MetOQAFile:MetSurfFile
    {


        public MetOQAFile()
        {
 

        }


        OQAHeader _header = new OQAHeader();



        List<OQARow> _data = new List<OQARow>();


        public DateTime StartTime
        {
            get {

                if (this._data[0]==null)
                {
                    return DateTime.Now;
                }

                return this._data[0].DateTime;
            

            }
        }



        public DateTime EndTime
        {
            get
            {

                if (this._data[this._data.Count-1] == null)
                {
                    return DateTime.Now;
                }

                return this._data[this._data.Count - 1].DateTime;



            }
        }


        public override bool ReadFile(string fname)
        {
            this.Read(fname);
            return true;

        }

        public override bool WriteFile(string fname)
        {
            this.Write(fname);
            return true;
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="fname"></param>
        protected void Read(string fname)
        {

            //取得行数啊，什么的，文件头啊，等等


            this._data.Clear();


            string[] split = new string[] {" "};

            using (StreamReader reader=new StreamReader (fname))
            {
                string line;


                //用于提高读取效率

                bool hasReadHeader = false;


                while ((line = reader.ReadLine()) != null)
                {



                   string[] arrary= line.Split(split,StringSplitOptions.RemoveEmptyEntries);

                    //判断是否是头文件,对这边不熟悉


                   if (arrary.Length>0)
                   {

                       string str = arrary[0];


                       if (!hasReadHeader)
                       {

                           if (str.Contains("%"))
                           {
                               //验证文件头
                               if (arrary[1]!="SURFACE")
                               {
                                   throw new Exception("不是标准的地面OQA气象文件");

                                   return;
                                   

                               }



                               continue;
                               
                           }


                           
                           if (str.Contains("@"))
                           {
                               //头行信息


                             


                               this._header.Location = arrary[2];

                               //先纬度，在经度

                               this._header.GeoPosition.GeoLat.SetValueWithString(arrary[3]);

                               this._header.GeoPosition.GeoLong.SetValueWithString(arrary[4]);

                              

                               this._header.TimeZone = Convert.ToInt32(arrary[5]);



                               hasReadHeader = true;


                           }



                       }
                       else
                       {

                           if (str.Contains("*"))
                           {
                               continue;
                           }



                           //开始读取正确的行信息

                           //需要一次性读取两行数据,因为两行文本，为一行标准数据


                           string lineTwo=line+" "+reader.ReadLine();

                           arrary=  lineTwo.Split(split,StringSplitOptions.RemoveEmptyEntries);



                           OQARow row = new OQARow();


                           row.DateTime = MetHelper.GetDateTime(arrary[0]);

                           row.Precipitation = Convert.ToInt32(arrary[1]);

                           row.SeeLevelPressture = Convert.ToInt32(arrary[2]);
                           row.StationPressture = Convert.ToInt32(arrary[3]);
                           row.CloudHeight = Convert.ToInt32(arrary[4]);
                           row.TotalCloud = Convert.ToInt32(arrary[5]);
                           row.ACL1 = Convert.ToInt32(arrary[6]);
                           row.ACL2 = Convert.ToInt32(arrary[7]);
                           row.ACL3 = Convert.ToInt32(arrary[8]);
                           row.ACL4 = Convert.ToInt32(arrary[9]);
                           row.ACL5 = Convert.ToInt32(arrary[10]);
                           row.ACL6 = Convert.ToInt32(arrary[11]);
                           row.PWVC = Convert.ToInt32(arrary[12]);
                           row.PWTH = Convert.ToInt32(arrary[13]);
                           row.ASOSSky = Convert.ToInt32(arrary[14]);
                           row.ASOSHeight = Convert.ToInt32(arrary[15]);
                           row.HVisibility = Convert.ToInt32(arrary[16]);
                           row.DryTemperature = Convert.ToInt32(arrary[17]);
                           row.WetTemperature = Convert.ToInt32(arrary[18]);
                           row.DewTemperature = Convert.ToInt32(arrary[19]);
                           row.RHumidity = Convert.ToInt32(arrary[20]);
                           row.WindDirection = Convert.ToInt32(arrary[21]);
                           row.WindSpeed = Convert.ToInt32(arrary[22]);



                           this._data.Add(row);
                        






 

                       }

                       
                       
                   }

                    //判断是否是具体的数据




 

                }



            }

       

           

 
        }


        protected void Write(string fname)
        {

            FileStream fs = new FileStream(fname, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            if (this._header != null)
            {
                sw.WriteLine("*% SURFACE");

                sw.WriteLine("*@  LOCATION " + this._header.Location + " " + this._header.GeoPosition.GeoLat.GetValueWithString()+" "+this._header.GeoPosition.GeoLong.GetValueWithString()+" "+this._header.TimeZone.ToString ());
                sw.WriteLine("*** EOH: END OF UPPERAIR QA HEADERS");
            }
            if (this._data.Count > 0)
            {
                for (int i = 0; i < this._data.Count; i++)
                {
                    OQARow row = this._data[i];
                    sw.Write(row.DateTime.ToString("yyMMddHH"));
                    sw.Write(" ");
                    sw.Write(row.Precipitation);
                    sw.Write(" ");
                    sw.Write(row.SeeLevelPressture);
                    sw.Write(" ");
                    sw.Write(row.StationPressture);
                    sw.Write(" ");
                    sw.Write(row.CloudHeight);
                    sw.Write(" ");
                    sw.Write(row.TotalCloud);
                    sw.Write(" ");
                    sw.Write(row.ACL1);
                    sw.Write(" ");
                    sw.Write(row.ACL2);
                    sw.Write(" ");
                    sw.Write(row.ACL3);
                    sw.Write(" ");
                    sw.Write(row.ACL4);
                    sw.Write(" ");
                    sw.WriteLine(row.ACL5);
                    sw.Write("        ");
                    sw.Write(row.ACL6);
                    sw.Write(" ");
                    sw.Write(row.PWVC);
                    sw.Write(" ");
                    sw.Write(row.PWTH);
                    sw.Write(" ");
                    sw.Write(row.ASOSSky);
                    sw.Write(" ");
                    sw.Write(row.ASOSHeight);
                    sw.Write(" ");
                    sw.Write(row.HVisibility);
                    sw.Write(" ");
                    sw.Write(row.DryTemperature);
                    sw.Write(" ");
                    sw.Write(row.WetTemperature);
                    sw.Write(" ");
                    sw.Write(row.DewTemperature);
                    sw.Write(" ");
                    sw.Write(row.RHumidity);
                    sw.Write(" ");
                    sw.Write(row.WindDirection);
                    sw.Write(" ");
                    sw.WriteLine(row.WindSpeed);
                }
            }

        }




    }
}
